package com.example.demo.permission.msg.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.common.RespBean;
import com.example.demo.permission.msg.pojo.MsgContent;
import com.example.demo.permission.msg.pojo.SysMsg;
import com.example.demo.permission.msg.service.SysMsgService;
import com.example.demo.permission.user.pojo.LtUser;
import com.example.demo.permission.user.service.LtUserService;

/**
 * 
 * <p>
 * 功能描述：系统消息控制层
 * </p>
 * 
 * @author lixh
 * @date 2019年3月13日 下午2:37:54
 * @version 1.0
 */

@RestController
@RequestMapping("/chat")
public class ChatController {

	@Autowired
	LtUserService ltUserService;

	@Autowired
	SysMsgService sysMsgService;

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public List<LtUser> getAllUser() {
		return ltUserService.getAllUserExceptAdmin();
	}

	@RequestMapping(value = "/nf", method = RequestMethod.POST)
	public RespBean sendNf(MsgContent content) {
		if (sysMsgService.sendMsg(content)) {
			return RespBean.ok("发送成功！");
		}
		return RespBean.error("发送失败！");
	}

	@RequestMapping(value = "sysmsgs")
	public List<SysMsg> getSysMsg(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		List<SysMsg> list = new ArrayList<SysMsg>();
		list = sysMsgService.getSysMsgByPage(page, size);
		return list;
	}
	
	
	@RequestMapping(value = "/markread", method = RequestMethod.PUT)
	public RespBean markRead(Long flag) {
		if (sysMsgService.markRead(flag)) {
			if (flag == -1) {
				return RespBean.ok("multiple");
			} else {
				return RespBean.ok("single");
			}
		} else {
			if (flag == -1) {
				return RespBean.error("multiple");
			} else {
				return RespBean.error("single");
			}
		}

	}

}
